package jinyilw.swing.rlaf.ui;

import jinyilw.swing.FontTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.rlaf.utils.RLafUtils;
import jinyilw.swing.visuals.text.JTextCompFunctionListener;

import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.ScrollPaneUI;
import javax.swing.plaf.basic.BasicEditorPaneUI;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author Jinyi
 */
public class REditorPaneUI extends BasicEditorPaneUI
{
	protected RFocusManager manager;
	protected boolean viewportSetted;
	protected JEditorPane t;
	private Object oldHonorDisplayProperties;
	private final RenderingConfig renderingConfig = new RenderingConfig();

	public static ComponentUI createUI(JComponent c)
	{
		return new REditorPaneUI();
	}

	@Override
	public void installUI(JComponent c)
	{
		t = (JEditorPane) c;
		super.installUI(c);
		t.setFont(FontTools.defaultFont);
		manager = RLafUtils.configTextComponent(t);
	}

	@Override
	protected void installListeners()
	{
		super.installListeners();
		t.addMouseListener(JTextCompFunctionListener.textCompFunctionListener);
	}

	@Override
	protected void uninstallListeners()
	{
		super.uninstallListeners();
		t.removeMouseListener(
				JTextCompFunctionListener.textCompFunctionListener);
	}

	@Override
	protected void installDefaults()
	{
		super.installDefaults();
		oldHonorDisplayProperties = getComponent()
				.getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
		// use component font and foreground for HTML text
		getComponent().putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
				true);
	}

	@Override
	protected void uninstallDefaults()
	{
		super.uninstallDefaults();
		getComponent().putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
				oldHonorDisplayProperties);
	}

	protected void viewportSetting(Graphics g, JComponent c)
	{
		g.setColor(RThemeColor.colorSet.transTBGColor);
		g.fillRect(2, 2, c.getWidth() - 4, c.getHeight() - 4);
		if (!viewportSetted)
		{
			viewportSetted = true;
			Component pc = c.getParent().getParent();
			ScrollPaneUI ui;
			if (pc instanceof JScrollPane && (ui = ((JScrollPane) pc)
					.getUI()) instanceof RScrollPaneUI)
			{
				RScrollPaneUI rUi = (RScrollPaneUI) ui;
				rUi.setFocusManager(manager);
				manager.setPaintComponent(pc);
				rUi.update(g, c);
			}
		}
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = g2d.getComposite();
		renderingConfig.setAllAAPainting(g2d);
		if (c.getParent() instanceof JViewport)
			viewportSetting(g, c);
		else
			RLafUtils.paintTextComponent(g, t, manager.isFocus);
		RThemeColor.customTextColorSetting(t);
		paint(g, c);
		renderingConfig.resetAllAAPainting(g2d);
		g2d.setComposite(composite);
	}

	@Override
	protected void paintBackground(Graphics g)
	{
	}
}
